<?php
header("Content-Type: application/json; charset=utf-8");
session_start();

// 权限验证：仅管理员可操作
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    echo json_encode(['status' => 'error', 'message' => '无权限操作']);
    exit;
}

require_once '../utils/cache.php';
$cache = new Cache();
$action = $_GET['action'] ?? '';

// 清空所有缓存
if ($action === 'clear') {
    $cache->clear();
    echo json_encode([
        'status' => 'success',
        'message' => '所有缓存已清空'
    ]);
    exit;
}

// 清除指定缓存（如文章列表）
if ($action === 'delete' && isset($_GET['key'])) {
    $cache->delete($_GET['key']);
    echo json_encode([
        'status' => 'success',
        'message' => "缓存【{$_GET['key']}】已删除"
    ]);
    exit;
}

echo json_encode(['status' => 'error', 'message' => '无效操作']);